package com.yunche.yunchebusiness.mvp.presenter

import android.annotation.SuppressLint
import com.yunche.yunchebusiness.base.mvp.BasePresenter
import com.yunche.yunchebusiness.mvp.contract.HomeContract
import com.yunche.yunchebusiness.mvp.model.HomeModel
import com.yunche.yunchebusiness.network.exception.ErrorStatus
import com.yunche.yunchebusiness.network.exception.ExceptionHandle

/**
 * Create by Crcker on 2018/12/27 16:01
 * Email: Crcker@163.com
 */


class HomePresenter : BasePresenter<HomeContract.View>(), HomeContract.Presenter {

    private val model by lazy {
        HomeModel()
    }

    @SuppressLint("CheckResult")
    override fun getHomePage() {
        checkViewAttached()
        mRootView?.showLoading()
        var disposable = model.getHomePage().subscribe({
            mRootView!!.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getHomeSuccess(it.datas!!)
                } else {
                    getHomeError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.getHomeError(ExceptionHandle.handleException(it))
        })
        addSubscription(disposable)
    }
}